package com.ssm.web.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public class AuthorityInterceptor extends HandlerInterceptorAdapter {
	
	private String[] url = {"/login.html", "/loginOut.html", "/rand"};
	
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		String uri = request.getRequestURI();
		for(String noRightPath : url) {
			if(uri.indexOf(noRightPath) != -1) {
				System.out.println("请求地址: " + uri + " ----- 此地址不需要过滤");
				return true;
			}
		}
		System.out.println("请求地址: " + uri);
		
		Object user = request.getSession().getAttribute("user");
		if (user == null) {
			request.setAttribute("msg", "请登录");
			request.getRequestDispatcher("/").forward(request, response);
			return false;
		}
		return true;
	}
}
